package com.zdy.im.user.dao;


import java.util.Collection;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.mapper.UserInfoMapper;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;


import com.zdy.im.user.entity.UserInfo;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;




@Service("userInfoDao")
public class UserInfoDao extends MyBatisBaseCacheDao<UserInfoMapper, UserInfo> {

    @Autowired
    private UserInfoMapper  userInfoMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInfo>  iPage =  new Query<UserInfo>().getPage(params);

        QueryWrapper<UserInfo> query =  new QueryWrapper<>();

        IPage<UserInfo> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<UserInfo> datas = new ArrayList<>();
        list.forEach(e -> datas.add(UserInfo.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
    }



    /**
     * 用户账号搜索，搜索名称，登陆名称，手机号码
      * @param page 用户账号搜索，搜索名称，登陆名称，手机号码
     * @return List<UserInfo>
     */
    public List<UserInfo> queryUserPageBySearch(BasePage page){

        String searchContent = "";
        if(page.getParamObject() != null){
            searchContent = page.getParamObject().toString();
        }
        boolean isNumber =   NumberUtils.isNumber(searchContent);
        QueryWrapper<UserInfo> query =  new QueryWrapper<>();
        if(!"".equals(searchContent)) {
            if (isNumber) {
                query.like("login_name", searchContent)
                        .or()
                        .like("mobile", searchContent);
            } else {
                query.like("login_name", searchContent)
                        .or()
                        .like("user_name", searchContent);
            }
        }
        Page<UserInfo> iPage = new Page<>(page.getPageNo(), page.getRealPageSize());
        iPage.setSearchCount(false);
        iPage.addOrder(OrderItem.desc("id"));
        IPage<UserInfo> result = this.page(iPage, query);
        if(null == result){
            return null;
        }
        List<UserInfo>  userInfoList = result.getRecords();
        return userInfoList;
    }

    public boolean getByLoginName(String loginName) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", loginName).eq("status",1);
        return CollectionUtils.isEmpty(this.list(queryWrapper));
    }

    public UserInfo getUserInfoByLoginName(String loginName) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", loginName).eq("status",1);
        List<UserInfo> imUserInfos = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserInfos)){
            return null;
        }
        return imUserInfos.get(0);
    }

    /**
     * 查询ids，用户信息
     * @param ids
     * @return
     */
    public List<UserInfo> getListByIds(Collection<Long> ids){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1)
                .in("id", ids);
        return this.list(queryWrapper);
    }
}